package fy.uap.operatorImpl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.LogFactory;

import com.seeyon.oainterface.exportData.form.ValueExport;

import fy.form.listen.dao.MyOrgManager;
import fy.form.listen.log.LogWriter;
import fy.form.listen.operate.AForm_Save_Operator;
import fy.uap.bean.FinanceInvalidSupplierBean;
import fy.uap.dao.InvalidFinanceSupplierDao;
import fy.util.MyUtil;

public class FinanceInvalidSupplierOperator extends AForm_Save_Operator {

	private static org.apache.commons.logging.Log log = LogFactory
			.getLog(FinanceInvalidSupplierOperator.class);

	private static final String formName = "财务供应商失效申请单";

	private List<String> invalidNameList = new ArrayList<String>();

	private List<String> invalidCodeList = new ArrayList<String>();

	private List<String> invalidDateList = new ArrayList<String>();

	private List<String> remarkList = new ArrayList<String>();

	private String memberCode;

	private String applicationTime;

	private String number;

	private List<String> corpList = new ArrayList<String>();

	private long memberID;

	public String getFormName() {
		return formName;
	}

	@Override
	protected void parseOtherField_FirstStep(
			com.seeyon.v3x.services.flow.bean.FlowExport flowExport) {
		memberID = flowExport.getFlowCreatePerson().getId();

		memberCode = MyOrgManager.getInstance().getMemberByID(memberID)
				.getCode();
		applicationTime = flowExport.getFlowCreateTime();
		// applicationTime = applicationTime.substring(0,
		// applicationTime.indexOf('.'));
	}

	@Override
	protected void parseValueExport(
			com.seeyon.v3x.services.form.bean.ValueExport ve) {
		String name = ve.getDisplayName();
		String value = ve.getValue();

		if ("失效供应商名称".equals(name)) {
			invalidNameList.add(MyUtil.replaceXMLString(value));
		} else if ("失效供应商编码".equals(name)) {
			invalidCodeList.add(MyUtil.replaceXMLString(value));
		} else if ("失效日期".equals(name)) {
			invalidDateList.add(MyUtil.getDate(MyUtil.replaceXMLString(value)));
		} else if ("备注".equals(name)) {
			remarkList.add(MyUtil.replaceXMLString(value));
		} else if ("单据编号".equals(name)) {
			number = value;
		} else if ("pk_corp".equals(name)) {
			value = value != null ? value.replace("key_", "") : "";
			if ("1004".equals(value) && corpList.indexOf("1025") == -1) {
				corpList.add("1025");
			} else if ("1005".equals(value) && corpList.indexOf("1024") == -1) {
				corpList.add("1024");
			} else if ("1006".equals(value) && corpList.indexOf("1026") == -1) {
				corpList.add("1026");
			} else if ("1009".equals(value) && corpList.indexOf("1032") == -1) {
				// 湖北/武汉荆门
				corpList.add("1032");
			} else if ("1012".equals(value)) {
				// 北京 导入保定
				if (corpList.indexOf("1036") == -1) {
					corpList.add("1036");
				}
			}
			if (corpList.indexOf(value) == -1)
				corpList.add(value);
		}
	}

	@Override
	protected boolean save() {
		ProviderMessageManager pmm = new ProviderMessageManager();
		pmm.setMemberID(memberID);
		if (invalidCodeList.size() == invalidDateList.size()
				&& invalidDateList.size() == invalidNameList.size()
				&& corpList.size() != 0) {
			List<FinanceInvalidSupplierBean> beans = new ArrayList<FinanceInvalidSupplierBean>();
			for (int j = 0, cSize = corpList.size(); j < cSize; j++) {
				for (int i = 0, size = invalidCodeList.size(); i < size; i++) {
					if (invalidCodeList.indexOf(invalidCodeList.get(i)) == i) {
						FinanceInvalidSupplierBean bean = new FinanceInvalidSupplierBean();
						bean.setPk_corp(corpList.get(j));
						bean.setCode(invalidCodeList.get(i));
						bean.setName(invalidNameList.get(i));
						bean.setDate(invalidDateList.get(i));
						beans.add(bean);
						bean = null;
					}
				}
			}

			if (beans.size() > 0) {

				InvalidFinanceSupplierDao dao = new InvalidFinanceSupplierDao();

				dao.setApplicationTime(applicationTime);
				dao.setMemberCode(memberCode);
				dao.setNumber(number);
				dao.setBeans(beans);

				if (dao.save()) {
					if (!dao.writeToErp()) {
						StringBuilder sb = new StringBuilder().append("表单《")
								.append(formName)
								.append("》数据导入到ERP系统发生异常，请手工导入。单据编号：")
								.append(number);
						pmm.sendMessage(sb.toString());
						log.error(sb.toString());
						LogWriter.writeLog(MyUtil.getRealPath(this.getClass()),
								sb.toString());
					}
					return true;
				} else {
					StringBuilder sb = new StringBuilder().append("表单《")
							.append(formName)
							.append("》数据导入到UAP和ERP系统发生异常，请手工导入。单据编号：")
							.append(number);
					pmm.sendMessage(sb.toString());
					log.error(sb.toString());
					LogWriter.writeLog(MyUtil.getRealPath(this.getClass()),
							sb.toString());
				}
			} else {
				StringBuilder sb = new StringBuilder().append("获取表单《")
						.append(formName)
						.append("》供应商名称和编码为空，无法自动导入，请用户手工导入。单据编号：")
						.append(number);
				pmm.sendMessage(sb.toString());
				log.error(sb.toString());
				LogWriter.writeLog(MyUtil.getRealPath(this.getClass()),
						sb.toString());
			}

		} else {
			StringBuilder sb = new StringBuilder()
					.append("获取表单《")
					.append(formName)
					.append("》的制造部、供应商名称和供应商编码异常，表单数据导入到UAP和ERP系统失败，请手工导入。单据编号：")
					.append(number);
			pmm.sendMessage(sb.toString());
			log.error(sb.toString());
			LogWriter.writeLog(MyUtil.getRealPath(this.getClass()),
					sb.toString());
		}
		return false;
	}
}
